$(function() {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.iframeAuto(index);
    
    $(".select-item-fmea-half").on("click", function() {
    	let $this = $(this);
        let id = $this.data("id");
        let $input = parent.clickTarget;
        let pageId = parent.layui.$("input[name='pageHalfId']").val();
        if (pageId == id){
        	parent.layer.msg("结构配置不能配置自己");
        }else if (parent.layui.$("td input[name='subId'][value='" + id + "']").size() == 0) {
        	let checkRes = checkDeadCycle(pageId, id);
        	if(checkRes === false){
        		$input.val($this.data('code'));
                $input.siblings("input[name='subId']").val(id);
                $input.parentsUntil('tbody').find('.delete-row').attr('id', id);
                parent.layer.close(index);
        	}else if (checkRes === true){
        		parent.layer.msg("结构配置不能A配B，B配A");
        	}else if (checkRes === "error"){
        		parent.layer.msg("服务器内部错误");
        	}
        } else if (parent.layui.$("td input[name='subId'][value='" + id + "']").size() > 0) {
            parent.layer.tips("已选择", $input.parents("table").find(".delete-row[id='" + id + "']"), {
                time : 3000
            });
        } 
    });
    
    $(".select-item-fmea-half-dfmea-step1").on("click", function() {
    	let $this = $(this);
    	let id = $this.data("id");
    	let $input = parent.clickTarget;
    	if (parent.layui.$("td input[name='halfId'][value='" + id + "']").size() == 0) {
    		let $form = $(parent.document).find("form");
    		let action = $form.attr("action");
    		let urlId = $form.find("input[name='id']").val();
    		parent.location.href = action.slice(0, action.lastIndexOf("/")) + "/step1/" + urlId + "?halfId=" + id;
    	} else {
    		parent.layer.tips("已选择", $input, {
    			time : 3000
    		});
    	}
    	parent.layer.close(index);
    });

    
    
    $(".select-item-fmea-product").on("click", function() {
    	let $this = $(this);
    	let id = $this.data("id");
    	let $input = parent.clickTarget;
    	if (parent.layui.$("td input[name='halfId'][value='" + id + "']").size() == 0) {
    		$input.val($this.data('code'));
    		$input.siblings("input[name='halfId']").val(id);
    		$input.parentsUntil('tbody').find('.delete-row').attr('id', id);
    	} else {
    		parent.layer.tips("已选择", $input.parents("table").find(".delete-row[id='" + id + "']"), {
    			time : 3000
    		});
    	}
    	parent.layer.close(index);
    });

    function checkDeadCycle(halfId, subHalfId){
    	let res = false;
    	$.ajax({
            type: "POST",
            url: "/fmea/fmea_half/dead_cycle",
            data: {halfId: halfId, subHalfId: subHalfId},
            dataType: "json",
            async: false,
            success: function(data) {
                if (data.status == "deadCycle") {
                	res = true;
                } 
            },
            error: function(request) {
            	res = "error";
            }
        });
    	return res;
    }
     

});